export class Person {
  private static instance: Person; // 静态属性，用于存储全局唯一的实例
  private name: string;
  private age: number;

  // 私有化了构造方法，外部无法通过 new Person() 来创建实例
  private constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }

  public static getInstance(name: string, age: number): Person {
    // 进行单例的判断
    if (Person.instance === null) {
      Person.instance = new Person(name, age);
    }
    // 没有进入上面的分支，说明已经 new 过一次，实例对象存储在 instance 中，直接返回即可
    return Person.instance;
  }

  sayHello(){
    console.log("Hello");
  }
}

const p1 = Person.getInstance("Tom", 18);
const p2 = Person.getInstance("John", 18);
console.log(p1 === p2); // true